package org.jod.spike.movimento;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ServerContext;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;


public class NavigationEntryPoint {

	private ServerContext sctx;
	private String mapPageUrl = "/spike/map.html";
	private long _interval=1000;
	
	private static Map _playersPosition = new HashMap();

	
	public NavigationEntryPoint() {


		

	}
	
	public void init() {
		playerPosition().setMap(0);
		playerPosition().setX(playerPosition().getX());
		playerPosition().setY(playerPosition().getY());
		updatePlayerPosition();
	}
	
	public void moveEast() {

		//playerPosition().setMap(0);
		playerPosition().setY(playerPosition().getY() + 1);
		//updatePlayerPosition();
		
		// solo un mock
		System.out.println("moveeast = " + playerPosition().getY());
		if (playerPosition().getY()>=10 ) {
			playerPosition().setFrommap(playerPosition().getMap());
			playerPosition().setMap(1);
			playerPosition().setY(0);
		}
	}

	public void moveSud() {

		//playerPosition().setMap(0);
		playerPosition().setX(playerPosition().getX() + 1);
		//updatePlayerPosition();
	}

	public void moveWest() {

		//playerPosition().setMap(0);
		playerPosition().setY(playerPosition().getY() - 1);
		//updatePlayerPosition();
	}

	public void moveNorth() {
		//playerPosition().setMap(0);
		playerPosition().setX(playerPosition().getX() - 1);
		//updatePlayerPosition();
	}

	public void moveNorthEast() {

		//playerPosition().setMap(0);
		playerPosition().setX(playerPosition().getX() - 1);
		playerPosition().setY(playerPosition().getY() + 1);
		//updatePlayerPosition();
	}

	public void moveSudEast() {
		//playerPosition().setMap(0);
		playerPosition().setX(playerPosition().getX() + 1);
		playerPosition().setY(playerPosition().getY() + 1);
		//updatePlayerPosition();
	}

	public void moveSudWest() {

		//playerPosition().setMap(0);
		playerPosition().setX(playerPosition().getX() + 1);
		playerPosition().setY(playerPosition().getY() - 1);
		//updatePlayerPosition();
	}

	public void moveNordWest() {
		//playerPosition().setMap(0);
		playerPosition().setX(playerPosition().getX() - 1);
		playerPosition().setY(playerPosition().getY() - 1);
		//updatePlayerPosition();
	}

	
	public PositionInfo playerData() {
		return playerPosition();
	}
	
	public void updatePlayerPosition() {

	    WebContext wctx = WebContextFactory.get();
	    sctx = ServerContextFactory.get(wctx.getServletContext());
		

		  ScriptBuffer script = new ScriptBuffer();
		  
		  System.out.println("updatePlayerPosition = " + playerPosition().getX());
		  System.out.println("updatePlayerPosition = " + playerPosition().getY());
		  
		  
		  script.appendScript("updatePlayer(")
		    .appendData(playerPosition())
		    .appendScript(");");

		    // Push change out to clients viewing the page
		    Collection sessions = new HashSet();        
		    sessions.addAll(sctx.getScriptSessionsByPage(mapPageUrl));
		   System.out.println(sessions.size());


		    //sessions.addAll(sctx.getScriptSessionsByPage(mapPageUrl));
		    System.out.println(sessions.size());
		    
		    Iterator sessionIterator = sessions.iterator();
		    
		    while (sessionIterator.hasNext()) {
		    	
		    	ScriptSession session = (ScriptSession)sessionIterator.next();
		    	session.addScript(script);
		    }
		    
		         
		}

	private PositionInfo playerPosition() {
		
		WebContext wctx = WebContextFactory.get();
		String user =  wctx.getHttpServletRequest().getUserPrincipal().getName();
		
		System.out.println("user = " + user);

		if (_playersPosition.get(user) == null) {
			 PositionInfo pinfo = new PositionInfo();
			 pinfo.setPlayername(user);
			_playersPosition.put(user, pinfo);

		}
		return (PositionInfo) _playersPosition.get(user);
	}



	public Collection updatePlayers() {
		System.out.println("updatePlayers called");
		return _playersPosition.values();
		
		/*
		WebContext wctx = WebContextFactory.get();
	    sctx = ServerContextFactory.get(wctx.getServletContext());
	    ScriptBuffer script = new ScriptBuffer();
	    
		  script.appendScript("updatePlayersPosition(")
		    .appendData(_playersPosition.values())
		    .appendScript(");");
		  
		    Collection sessions = new HashSet();        
		    sessions.addAll(sctx.getScriptSessionsByPage(mapPageUrl));
		    
	    Iterator sessionIterator = sessions.iterator();
		    
		    while (sessionIterator.hasNext()) {
		    	
		    	ScriptSession session = (ScriptSession)sessionIterator.next();
		    	session.addScript(script);
		    }
		    */
	}
}
